package indi.h.devmg.supervise.cmd;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import indi.h.devmg.supervise.cmd.configs.CmdConfig;
import indi.h.devmg.util.IOUtil;

/**
 * cmd工具类
 * 
 * @author hdy
 * 
 */
public class CmdUtil implements Runnable{
	private CmdConfig cmdConfig;
	private InputStream i;
	private InputStreamReader ir;
	private BufferedReader b;
	private Process e;
	final static Logger l = LoggerFactory.getLogger(CmdUtil.class);

	private Process getE() {
		return e;
	}

	public CmdUtil() {
		// TODO Auto-generated constructor stub
	}

	public CmdUtil(CmdConfig nmapConfig) {
		this.cmdConfig = nmapConfig;
	}

	public void setNmapConfig(CmdConfig nmapConfig) {
		this.cmdConfig = nmapConfig;
	}

	public CmdConfig getNmapConfig() {
		return cmdConfig;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		StopThread s = new StopThread(this, this.cmdConfig.getMaxRuntime());
		try {
			s.start();
			ProcessBuilder p = new ProcessBuilder(this.cmdConfig.getCommands());
			p.redirectErrorStream(true);
			e = p.start();
			i = e.getInputStream();
			ir = new InputStreamReader(i);
			b = new BufferedReader(ir);
			String l = b.readLine();
			while (l != null) {
				this.cmdConfig.getDataOperation().operation(l);
				l = b.readLine();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			if (s.isAlive())
				s.interrupt();
			this.cmdConfig.getDataOperation().end();
		}
	}

	public void close() throws IOException {
		// TODO Auto-generated method stub
		IOUtil.close(this.getClass(), b, ir, i);
	}

	private class StopThread extends Thread {
		private CmdUtil cmdUtil;
		private long stopTime;

		public StopThread(CmdUtil nmapUtil, long stopTime) {
			this.cmdUtil = nmapUtil;
			this.stopTime = stopTime;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			try {
				if (this.stopTime > 0) {
					Thread.sleep(this.stopTime);
					this.cmdUtil.getE().destroy();
					this.cmdUtil.close();
				}
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				l.info("command:"+Arrays.asList(this.cmdUtil.getNmapConfig().getCommands())+"---time not out");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				l.warn("command:"+Arrays.asList(this.cmdUtil.getNmapConfig().getCommands())+"---run time out");
			}
		}
	}

	/**
	 * test new String[] { "C:/NMAP/nmap-7.40-win32/nmap.exe", "-v","-sn", "-n",
	 * "192.168.0.165/24" } new String[] { "C:/NMAP/nmap-7.40-win32/nmap.exe",
	 * "-T4", "-O", "-sV","-n", "192.168.0.223" }
	 */
	// public static void main(String[] args) {
	// CmdConfig nmapConfig2 = new CmdConfig(new String[] {
	// "C:/NMAP/nmap-7.40-win32/nmap.exe", "-T4", "-O", "-sV", "-n",
	// "--script=firewalk", "--traceroute", "192.168.0.223" },
	// new DataOperation<String>() {
	//
	// @Override
	// public void operation(String data) {
	// // TODO Auto-generated method stub
	// System.out.println(data);
	// }
	//
	// @Override
	// public void end() {
	// // TODO Auto-generated method stub
	//
	// }
	// });
	// // nmapConfig2.setMaxRuntime(5000);
	// new CmdUtil(nmapConfig2).run();
	// }
}
